package com.hcms;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.HashMap;
import java.util.Map;

public class MybatisPlusCodeGenerator {

    public static void main(String[] args) {
        new MybatisPlusCodeGenerator().generator();
    }

    public void generator() {
        String url = "jdbc:mysql://host-mysql:3306/hcms?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true";
        String username = "root";
        String password = "root";
        String projectPath = System.getProperty("user.dir");
        String generatorPath = projectPath + "/hcms-generator/src/main/java";
        String[] include = new String[]{
//                "sys_admin",
//                "sys_role",
//                "sys_permission",
//                "sys_admin_login_log",
//                "sys_admin_role_relation",
//                "sys_role_permission_relation",
//                "sys_admin_permission_relation",
//                "sys_object_storage",
//                "sys_menu",
//                "sys_app_setting",
//                "cms_article",
//                "cms_cate",
//                "cms_page",
                "cms_module",
        };
        FastAutoGenerator
                .create(url, username, password)
                .globalConfig(builder -> {
                    builder
                            .fileOverride()
                            .outputDir(generatorPath) // 指定输出目录
                            .author("achenxw@126.com") // 设置作者
                            .disableOpenDir()
                            .dateType(DateType.ONLY_DATE)
                            .enableSwagger(); // 开启 swagger 模式
                })
                .packageConfig(builder -> {
                    Map<OutputFile, String> pathInfo = new HashMap<>();
                    pathInfo.put(OutputFile.xml, generatorPath + "/com/hcms/mybatis/mapper");
//                    pathInfo.put(OutputFile.entity, generatorPath + "/com/hcms/mybatis/entity");
//                    pathInfo.put(OutputFile.mapper, generatorPath + "/com/hcms/mybatis/mapper");
//                    pathInfo.put(OutputFile.service, generatorPath + "/com/hcms/admin/service");
//                    pathInfo.put(OutputFile.serviceImpl, generatorPath + "/com/hcms/admin/service/impl");
//                    pathInfo.put(OutputFile.controller, generatorPath + "/com/hcms/admin/controller");
                    builder
                            .parent("com.hcms.mybatis") // 设置父包名
                            .pathInfo(pathInfo);
                })
                .strategyConfig(builder -> {
                    builder
                            .addInclude(include) // 设置需要生成的表名
                            //entity配置
                            .entityBuilder()
                            .enableLombok()
                            //controller配置
                            .controllerBuilder()
                            .enableRestStyle();
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}
